#include <sys/types.h>
#include "apue.h"
#include <sys/stat.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define FIFO_NAME "my_fifo"

int main()
{
    int     fd;
    int     res;
    int     open_mode = O_RDONLY;
    char    buffer[PIPE_BUF + 1];
    int     bytes = 0;

    printf("Consumer Program beginning...\n");
    memset(buffer, '\0', sizeof(buffer));

    fd = open(FIFO_NAME, open_mode);
    printf("Process %d result %d\n", getpid(), fd);

    if (fd != -1)
    {
        res = 0;
        do
        {
            bytes += res;
            res = read(fd, buffer, PIPE_BUF + 1);
        } while (res > 0);
        close(fd);
    }
    else
    {
        exit(1);
    }

    printf("Process %d finished, %d bytes read\n", getpid(), bytes);
    exit(0);
}
